package com.updesignsz.admin.mapper;

import com.updesignsz.admin.vo.form.ProjectsForm;
import com.updesignsz.commom.entity.Projects;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 项目
 * Creation date 2020-8-8 15:07:53
 */
@Repository
@Mapper
public interface ProjectsMapper {


    @Select({"<script>",
            "select * from projects ",
            "<where>",
            "<if test=\"type != null\" >AND type = #{type}</if>",
            "</where>",
            "order by sequence desc, created_date desc",
            "</script>"
    })
    @Results({
            @Result(property = "typeEntity", column = "type", many = @Many(select = "com.updesignsz.admin.mapper.TypeMapper.getById"))
    })
    List<Projects> listAll(Projects projects);


    @Select("select * from projects where id = #{id}")
    Projects getById(@Param("id") Integer id);

    @Insert({
            "insert into projects(name, `type`, cover, cover_thumbnail, owner_name, architecture, view_design,",
            "complete_date, project_image, area, sequence, is_removed, created_date, updated_date) ",
            "values (#{name},#{type},#{cover},#{coverThumbnail},#{ownerName},#{architecture},#{viewDesign},#{completeDate},",
            "#{projectImage},#{area},1,0,NOW(),NOW())"
    })
    @Options(useGeneratedKeys = true, keyColumn = "id")
    void saveProjects(Projects type);


    @Update({"<script>",
            "update projects <set>",
            "<if test=\"name != null\" >name = #{name},</if>",
            "<if test=\"type != null\" >type = #{type},</if>",
            "<if test=\"cover != null\" >cover = #{cover},</if>",
            "<if test=\"coverThumbnail != null\" >cover_thumbnail = #{coverThumbnail},</if>",
            "<if test=\"ownerName != null\" >owner_name = #{ownerName},</if>",
            "<if test=\"architecture != null\" >architecture = #{architecture},</if>",
            "<if test=\"viewDesign != null\" >view_design = #{viewDesign},</if>",
            "<if test=\"completeDate != null\" >complete_date = #{completeDate},</if>",
            "<if test=\"area != null\" >area = #{area},</if>",
            "<if test=\"projectImage != null\" >project_image = #{projectImage},</if>",
            "<if test=\"sequence != null\" >sequence = #{sequence},</if>",
            "updated_date = NOW()",
            "</set> where id = #{id}",
            "</script>"
    })
    void updateProjects(Projects type);

    @Delete("delete from projects where id = #{id}")
    void deleteProjects(@Param("id") Integer id);
}